public class MyCircularQueue2 {
    private int[] array;
    private int front; //队头指针
    private int rear;  //队尾指针
    private boolean isFull; //标记队列是否为满

    public MyCircularQueue2(int k) {
        array = new int[k];// 无需浪费空间
        front = 0;
        rear = 0;
        isFull = false;
    }

    //入队操作
    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }
        array[rear] = value;
        rear = (rear+1) % array.length;
        //入队后检查是否满
        if(rear == front) {
            isFull = true;
        }
        return true;
    }

    //出队操作
    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        front = (front+1) % array.length;
        //出队后队列一定非满
        isFull = false;
        return true;
    }

    //获取队头元素
    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        //处理 rear = 0 的情况
        return array[(rear - 1 + array.length) % array.length];
    }

    //获取队尾元素
    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        //处理 rear = 0 的情况
        return array[rear - 1 + array.length] % array.length;
    }

    //判断队列是否为空
    public boolean isEmpty() {
        return front == rear && isFull;
    }

    //判断队列是否已满
    public boolean isFull() {
        return isFull;
    }
}
